local SpriteUtils = {}

function SpriteUtils:loadAllTextures()
   if __atlases ~= nil then return end
   __atlases = {}--global

   -- for i=1,17 do
   --    local spriteSheetInfo = require('resources.sprites.atlases.kidsAndZombies.kid' .. i)
   --    local spriteSheet     = graphics.newImageSheet(string.format("resources/sprites/atlases/kidsAndZombies/kid%d.png",i), spriteSheetInfo:getSheet())

   --    table.insert(__atlases,{spriteSheet=spriteSheet,info=spriteSheetInfo})
   -- end

   -- local spriteSheetInfo = require('resources.sprites.atlases.scenegame0')
   -- local spriteSheet = graphics.newImageSheet("resources/sprites/atlases/scenegame0.png", spriteSheetInfo:getSheet())

   -- table.insert(__atlases,{spriteSheet=spriteSheet,info=spriteSheetInfo})
end

function SpriteUtils:getFrameSequence(atlasInfo,prefix,a,b,format)
   format = format or '%d'

   local sequence = {}
   for i=a,b do
      local key = string.format(prefix..format,i)
      local index = atlasInfo:getFrameIndex(key)
      assert(index ~= nil,string.format('Key %s not found',key))
      table.insert(sequence,index)
   end

   return sequence
end

function SpriteUtils:getKidSequenceData(kidClass)
   local kidPrefix = nil
   if kidClass == KidConstants.SOCCER_CHILD       then kidPrefix = 'c1' end
   if kidClass == KidConstants.SLINGSHOT_CHILD    then kidPrefix = 'c2' end
   if kidClass == KidConstants.BAZOOKA_CHILD      then kidPrefix = 'c3' end
   if kidClass == KidConstants.EXTINGUISHER_CHILD then kidPrefix = 'c4' end
   if kidClass == KidConstants.TENNIS_CHILD       then kidPrefix = 'c5' end
   if kidClass == KidConstants.GOLF_CHILD         then kidPrefix = 'c6' end

   assert(kidPrefix)

   local sequenceData = {}
   for angleRight = 90,-90,-45 do
      local angle = angleRight % 360
      assert(angle >= 0)
      local prefix = string.format('%s-%dfire',kidPrefix,angle)
      local atlas = SpriteUtils:findAtlasByPrefix(prefix)
      assert(atlas)

      local attackFramePeriod = SpriteConstants.ATTACK_FRAME_PERIOD
      if kidClass == KidConstants.EXTINGUISHER_CHILD then attackFramePeriod = SpriteConstants.EXTINGUISHER_FRAME_PERIOD end
      if kidClass == KidConstants.GOLF_CHILD         then attackFramePeriod = SpriteConstants.GOLF_FRAME_PERIOD end
      table.insert(sequenceData,
         {  name   ="attack_"..angle,
            sheet  = atlas.spriteSheet,
            frames = self:getFrameSequence(atlas.info,prefix,1,18),
            time   = attackFramePeriod,
            loopCount=1
         }
      )
   end

   local otherAnimations = {
      {abbrev = 'cel',count=14,loop=0,time=SpriteConstants.CELEBRATION_FRAME_PERIOD,name='celebration'},--Celebration
      {abbrev = 'def',count=12,loop=1,time=SpriteConstants.DEFENSE_FRAME_PERIOD,    name='defense'},--Defense
      {abbrev = 'in' ,count=10,loop=1,time=SpriteConstants.ENTRANCE_FRAME_PERIOD,   name='entrance'},--Entrance
      {abbrev = 'run',count=16,loop=0,time=SpriteConstants.RUNNING_FRAME_PERIOD,    name='running'},--Running on Defeat
      {abbrev = 'sb' ,count=9 ,loop=0,time=SpriteConstants.STAND_BY_FRAME_PERIOD,   name='standBy'}--Stand by
   }
   for i=1,#otherAnimations do
      local name   = otherAnimations[i].name
      local abbrev = otherAnimations[i].abbrev
      local count  = otherAnimations[i].count
      local loop   = otherAnimations[i].loop
      local time   = otherAnimations[i].time

      local prefix = string.format('%s-%s',kidPrefix,abbrev)
      local atlas = SpriteUtils:findAtlasByPrefix(prefix)
      assert(atlas)
      assert(atlas.info:getFrameIndex(prefix .. (count+0)) ~= nil,prefix..count..' is nil')
      assert(atlas.info:getFrameIndex(prefix .. (count+1)) == nil)
      table.insert(sequenceData,
         {  name   = name,
            sheet  = atlas.spriteSheet,
            frames = self:getFrameSequence(atlas.info,prefix,1,count),
            time   = time,
            loopCount=loop
         }
      )
   end

   return sequenceData
end

function SpriteUtils:findAtlasByPrefix(prefix)
   for key,atlas in pairs(__atlases) do
      if atlas.info:getFrameIndex(prefix..1) ~= nil then
         return atlas
      end
   end
   assert(false,'Cannot find [' .. prefix .. '] prefix')
end

return SpriteUtils